//
//  DemoUpnpCell.m
//  wAudioShare
//
//  Created by 赵帅 on 13-12-12.
//  Copyright (c) 2013年 赵帅. All rights reserved.
//

#import "DemoUpnpCell.h"

@interface DemoUpnpCell()
{
    NSString *ipUrl;
}
@property (assign) IBOutlet UILabel * statusLabel;


@property (assign) IBOutlet UIButton * connectButton;
@property (assign) IBOutlet UIButton * disConnectButton;

@end

@implementation DemoUpnpCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code

    }
    return self;
}

-(void)awakeFromNib
{
    //self.expandButton.hidden = YES;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

-(void)expand:(BOOL)expand withAnimation:(BOOL)animation
{
    if(animation == YES)
    {
        [self performSelectorOnMainThread:@selector(expandAnimation:) withObject:[NSNumber numberWithBool:expand] waitUntilDone:YES];
    }
    else
    {
        if(expand == NO)
        {
            _expandButton.transform = CGAffineTransformMakeRotation(0 * (M_PI / 180.0f));
        }
        else
        {
            _expandButton.transform = CGAffineTransformMakeRotation(180 * (M_PI / 180.0f));
        }
    }
}

-(IBAction)changeNameButtonPressed:(id)sender
{
    if ([_delegate respondsToSelector:@selector(changeNameButtonPressed)])
    {
        [_delegate changeNameButtonPressed];
    }
}

-(IBAction)expandButtonPressed:(id)sender
{
    if([_delegate respondsToSelector:@selector(expandButtonPressed:)])
    {
        [_delegate expandButtonPressed:self];
    }
}

-(IBAction)connectButtonPressed:(id)sender
{
    if([_delegate respondsToSelector:@selector(connectButtonPressed)])
    {
        [_delegate connectButtonPressed];
    }
}

-(IBAction)disconnectButtonPressed:(id)sender
{
    if([_delegate respondsToSelector:@selector(disConnectButtonPressed)])
    {
        [_delegate disConnectButtonPressed];
    }
}

-(IBAction)remoteControlButtonPressed:(id)sender
{
    if([_delegate respondsToSelector:@selector(remoteControlButtonPressed)])
    {
        [_delegate performSelectorOnMainThread:@selector(remoteControlButtonPressed) withObject:nil waitUntilDone:YES];
    }
}

-(void)isConnected:(BOOL)isAnimation
{
    if(isAnimation == YES)
    {
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.5];
        [UIView setAnimationDelegate:self];
        
        self.connectButton.alpha = 0;
        self.disConnectButton.alpha = 1;
        self.remoteControlButton.alpha = 1;
        self.changeDeviceNameBtn.alpha = 1;
        [UIView commitAnimations];
    }
    else
    {
        self.connectButton.alpha = 0;
        self.disConnectButton.alpha = 1;
        self.remoteControlButton.alpha = 1;
        self.changeDeviceNameBtn.alpha = 1;
    }
}

-(void)isDisConnected:(BOOL)isAnimation
{
    if(isAnimation == YES)
    {
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.5];
        [UIView setAnimationDelegate:self];
        
        self.connectButton.alpha = 1;
        self.disConnectButton.alpha = 0;
        self.remoteControlButton.alpha = 0;
        self.uuidLabel.alpha = 1;
        [UIView commitAnimations];
    }
    else
    {
        self.connectButton.alpha = 1;
        self.disConnectButton.alpha = 0;
        self.remoteControlButton.alpha = 0;
    }
}

-(void)hideAll
{
    self.connectButton.alpha = 0;
    self.disConnectButton.alpha = 0;
    self.remoteControlButton.alpha = 0;
    self.changeDeviceNameBtn.alpha = 0;
}

-(void)setStatus:(int)status withVersion:(int)version
{
    //NSString *uuid = self.uuidLabel.text;
    if (status == 1) {
 
        self.statusLabel.text = @"在线";
        self.disConnectButton.enabled = YES;
        
        if (self.ipLabel.text.length > 0) {
            self.remoteControlButton.enabled = YES;
            self.changeDeviceNameBtn.enabled = YES;
            
        }else {
       
            self.remoteControlButton.enabled = YES;
            self.changeDeviceNameBtn.enabled = NO;
        }
        
    }else if (status == 0) {
        
        self.statusLabel.text = @"离线";
        self.disConnectButton.enabled = YES;
        if (self.ipLabel.text.length > 0) {
            
            self.remoteControlButton.enabled = YES;
            self.changeDeviceNameBtn.enabled = YES;
        }
        else
        {
            self.remoteControlButton.enabled = NO;
            self.changeDeviceNameBtn.enabled = NO;
        }
       
        
    }else if(status < 0)
    {
        if(version >= 2)
        {
            self.statusLabel.text = @"可关联";
            self.connectButton.enabled = YES;
            
        }
        else
        {
            self.statusLabel.text = @"版本过低";
            self.connectButton.enabled = NO;
        }
        
        self.disConnectButton.enabled = YES;
        self.remoteControlButton.enabled = YES;
    }
    
    
/*
    if(status == 1)
    {
        self.statusLabel.text = @"在线";
        self.connectButton.enabled = YES;
        self.disConnectButton.enabled = YES;
        self.remoteControlButton.enabled = YES;
    }
    else if(status == 0)
    {
        self.statusLabel.text = @"离线";
        //局域网内remote执行
        ipUrl = [Utitlity judgeipUrl];
        if (ipUrl)
        {
            self.connectButton.enabled = YES;
        }
        else
        {
            self.remoteControlButton.enabled = NO;
        }
        self.connectButton.enabled = YES;
        self.disConnectButton.enabled = YES;

    }
    else if(status < 0)
    {
        if(version >= 2)
        {
            self.statusLabel.text = @"可关联";
            self.connectButton.enabled = YES;
    
        }
        else
        {
            self.statusLabel.text = @"版本过低";
            self.connectButton.enabled = NO;
        }
        
        self.disConnectButton.enabled = YES;
        self.remoteControlButton.enabled = YES;
    }
*/
}

#pragma mark - private methods
-(void)expandAnimation:(NSNumber *)isExpand
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.3];
    [UIView setAnimationDelegate:self];
    
    if([isExpand boolValue] == NO)
    {
        _expandButton.transform = CGAffineTransformMakeRotation(0 * (M_PI / 180.0f));
    }
    else
    {
        _expandButton.transform = CGAffineTransformMakeRotation(180 * (M_PI / 180.0f));
    }
    
    [UIView commitAnimations];
}

@end
